package com.youkeda.yunmusic.web.control;

import com.youkeda.yunmusic.web.model.Comment;
import com.youkeda.yunmusic.web.model.SongInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SongInfoControl {
    private static Map<String, SongInfo> songInfoMap = new HashMap<>();

    @Autowired
    private CommentPostControl commentPostControl;

    @RequestMapping(value = "/songinfo")
    public String index(String songId, ModelMap modelMap) {
        if (songId == null || !songInfoMap.containsKey(songId)) {
            songId = "440208476";
        }


//        if(songId==null)
//        {
//            songId="440208476";
//        }
//
//        if(!songInfoMap.containsKey(songId)){
//            songId="440208476";
//        }
        SongInfo songInfo = songInfoMap.get(songId);
        modelMap.addAttribute("song", songInfo);
        List<Comment> comments = commentPostControl.getComments(songId);
        modelMap.addAttribute("comments",comments);

        return "index";
    }

    @RequestMapping(value = "/songinfo/get")
    @ResponseBody
    public SongInfo get(String songId) {
        SongInfo songInfo = songInfoMap.get(songId);
        return songInfo;
    }

    @PostConstruct
    public void init() {
        //that girl这首歌
        SongInfo songInfo = new SongInfo();
        songInfo.setId("440208476");
        songInfo.setName("That girl");
        songInfo.setSinger("Olly Murs");
        songInfo.setAlbumName("24 HRS (Deluxe) ");
        songInfo.setCommentCount(1200);
        songInfo.setLyrics("There's a girl but I let her get away <br/> 曾经心爱的女孩 我却让她擦肩而过 <br/> It's all my fault cause pride got in the way <br/>自尊心作祟 一切都是我的错 <br/> And I'd be lying if I said I was ok<br/> 若说无事 其实只是谎言未戳破<br/>" +
                " About that girl the one I let get away <br/> 那个女孩 我们曾擦肩而过<br/>");
        songInfo.setAlbumImg("http://p2.music.126.net/5HEwV-KwHoazXJ2CAHy1XA==/18259589602949147.jpg?param=130y130");
        songInfoMap.put("440208476", songInfo);
        //Something Just Like This 这首歌
        songInfo = new SongInfo();
        songInfo.setId("461347998");
        songInfo.setName("Something Just Like This ");
        songInfo.setSinger("The Chainsmokers / Coldplay");
        songInfo.setAlbumName("Something Just Like This");
        songInfo.setCommentCount(2200);
        songInfo.setLyrics("I've been reading books of old<br/> 我曾饱览古老的书籍 <br/> The legends and the myths <br/>那些传说与神话<br/> Achilles and his gold<br/> 阿喀琉斯和他的战利品<br/>" +
                " Hercules and his gifts <br/> 大力神与他的天赋<br/>");
        songInfo.setAlbumImg("http://p1.music.126.net/ggnyubDdMxrhpqYvpZbhEQ==/3302932937412681.jpg?param=130y130");
        songInfoMap.put("461347998", songInfo);
    }
}
